package com.ahoi.common.base;


import com.alibaba.fastjson.JSON;
import org.apache.tomcat.util.http.fileupload.IOUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 *
 */
public class ResponseJson implements Serializable {

    private int status;
    private String message;
    private Object data;
    static final Object EMPTY_OBJECT = new Object();

    ResponseJson(ResponseCode code, Object data) {
        this.status = code.getStatus();
        this.message = code.getMessage();
        this.data = data == null ? EMPTY_OBJECT : data;
    }

    public static void writeSuccess(HttpServletResponse response, Object obj) {
        ResponseJson json = new ResponseJson(ResponseCode.SUCCESS, obj);
        writeJson(response, json);
    }

    public static void writeSuccess(HttpServletResponse response) {
        writeSuccess(response, null);
    }

    public static void writeError(HttpServletResponse response, ResponseCode code, Object obj) {
        ResponseJson json = new ResponseJson(code, obj);
        writeJson(response, json);
    }

    public static void writeError(HttpServletResponse response, String message) {
        ResponseCode code = ResponseCode.BIZERROR;
        code.setMessage(message);
        writeError(response, code, null);
    }

    public static void writeError(HttpServletResponse response) {
        writeError(response, ResponseCode.ERROR, null);
    }

    public static void writeNoLogin(HttpServletResponse response, Object obj) {
        writeError(response, ResponseCode.NOLOGIN, obj);
    }

    protected static void writeJson(HttpServletResponse response, ResponseJson json) {
        PrintWriter out = null;
        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/html; charset=utf-8");
            String _json = JSON.toJSONString(json);
            out = response.getWriter();
            out.append(_json);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }

    public static void renderDownload(HttpServletRequest request, HttpServletResponse response, final File file,
                                      final String fileName) throws IOException {
        InputStream in = new FileInputStream(file);
        renderDownload(request, response, fileName, in);
    }

    public static void renderDownload(HttpServletRequest request, HttpServletResponse response, final String fileName,
                                      InputStream in) throws IOException {
        String mimetype = "application/octet-stream";
        String encodeName = fileName;
        String ua = request.getHeader("User-Agent");
        try {
            encodeName = new String(encodeName.getBytes("GBK"), "ISO-8859-1");
        } catch (Exception e) {
        }
        mimetype = " application/octet-stream ";

        response.reset();
        response.setContentType(mimetype);
        response.setHeader("Content-Disposition", "attachment; filename=\"" + encodeName + "\"");
        OutputStream out = null;
        try {
            out = response.getOutputStream();
            byte[] buffer = new byte[4096];
            int i = -1;
            while ((i = in.read(buffer)) != -1) {
                out.write(buffer, 0, i);
            }
            response.flushBuffer();
        } finally {
            IOUtils.closeQuietly(out);
            IOUtils.closeQuietly(in);
        }
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}
